﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;

namespace exsportance.Silverlight.Common.Helper
{
    public static class Storage
    {
        public static T GetSetting<T>(string name)
        {
            return GetSetting<T>(name, default(T));
        }

        public static T GetSetting<T>(string name, T defaultValue)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(name) && IsolatedStorageSettings.ApplicationSettings[name] != null)
            {
                return (T)IsolatedStorageSettings.ApplicationSettings[name];
            }
            else
                return defaultValue;
        }

        public static void SetSetting<T>(string name, T data)
        {
            IsolatedStorageSettings.ApplicationSettings[name] = data;
        }

        public static void SaveData<T>(T data, string fileName)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {

                if (store.FileExists(fileName))
                    store.DeleteFile(fileName);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                    serializer.WriteObject(memoryStream, data);

                    CheckAvailableSize(memoryStream.Length);

                    memoryStream.Close();
                }

                using (IsolatedStorageFileStream stream = store.CreateFile(fileName))
                {

                    DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                    serializer.WriteObject(stream, data);
                    stream.Close();
                }
            }
        }

        public static void SaveStringData(string data, string fileName)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(fileName))
                    store.DeleteFile(fileName);

                using (IsolatedStorageFileStream stream = store.CreateFile(fileName))
                {
                    using (StreamWriter sw = new StreamWriter(stream))
                    {
                        sw.Write(data);
                        sw.Close();
                    }
                    stream.Close();
                }
            }
        }

        public static T LoadData<T>(string fileName)
        {
            T data = default(T);

            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(fileName))
                {
                    using (IsolatedStorageFileStream stream = store.OpenFile(fileName, FileMode.Open))
                    {
                        DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                        data = (T)serializer.ReadObject(stream);
                        stream.Close();
                    }
                }
            }
            return data;
        }

        public static string LoadStringData(string fileName)
        {
            string data = "";
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(fileName))
                {

                    using (IsolatedStorageFileStream isfs = store.OpenFile(fileName, FileMode.Open))
                    {
                        using (StreamReader sr = new StreamReader(isfs))
                        {

                            string lineOfData = String.Empty;
                            while ((lineOfData = sr.ReadLine()) != null)
                                data += lineOfData;
                        }
                        isfs.Close();
                    }
                }
            }
            return data;
        }

        public static bool CheckAvailableSize(long spacedNeeded)
        {
            bool result = false;

            using (var store = IsolatedStorageFile.GetUserStoreForSite())
            {
                long currentSpace = store.AvailableFreeSpace;

                if (currentSpace < spacedNeeded)
                {
                    result = store.IncreaseQuotaTo(store.Quota + (spacedNeeded - currentSpace) + (20 * 1024));
                }
                else
                    result = true;
            }

            return result;
        }

        /// <summary>
        /// Increase Quota
        /// </summary>
        /// <param name="newSize"></param>
        /// <returns></returns>
        public static bool IncreaseQuota(long newSize)
        {
            bool result = false;
            long currentSpace = 0;

            using (var store = IsolatedStorageFile.GetUserStoreForSite())
            {
                currentSpace = store.AvailableFreeSpace;

                if (currentSpace < newSize)
                {
                    result = store.IncreaseQuotaTo(newSize);
                }
            }

            return result;
        }

    }
}
